<template>
  <div style="padding: 15px">
    <div style="margin-bottom: 15px;">
      <Input style="width: 200px;" v-model="keyword" placeholder="请输角色名"/>
      <Button type="success" @click="searchOrg">搜索</Button>
      <Button @click="add" type="primary" style="float: right;">添加角色</Button>
    </div>
    <Table border :columns="roleTitle" :data="roleList"></Table>
    <page-com :total="total" @page-change="pageChange"></page-com>
    <Modal v-model="identity" :title="modalTitle" :width="750" :mask-closable="false" @on-ok="submit" @on-cancel="cancel">
      <Form :model="addForm" :label-width="80">
        <FormItem label="角色名称">
          <Input v-model="addForm.roleName" placeholder="请输入身份名称" />
        </FormItem>
        <FormItem label="角色描述">
          <Input v-model="addForm.comments" type="textarea" :autosize="{minRows: 2,maxRows: 3}" placeholder="角色描述"/>
        </FormItem>
        <!--<FormItem label="设置禁启">-->
          <!--<i-switch size="large" v-model="addStatus">-->
            <!--<span slot="open">启用</span>-->
            <!--<span slot="close">禁用</span>-->
          <!--</i-switch>-->
        <!--</FormItem>-->
      </Form>
    </Modal>
  </div>
</template>

<script>
  import PageCom from '_c/page'
  const url = '/api/role'
  export default {
    name: 'users',
    data () {
      return {
        identity: false,
        addForm: {},
        modalTitle: '',
        number: 0,
        total: 0,
        keyword: '',
        pageSize: 10,
        pageNumber: 0,
        roleTitle: [
          {
            title: '角色ID',
            align: 'center',
            tooltip: true,
            key: 'roleId'
          },
          {
            title: '角色名称',
            align: 'center',
            tooltip: true,
            key: 'roleName'
          },
          {
            title: '角色描述',
            align: 'center',
            tooltip: true,
            key: 'comments'
          },
          {
            title: '添加时间',
            align: 'center',
            tooltip: true,
            render: (h, params) => {
              return h('div', this.$formatDate(new Date(params.row.createTime), 'yyyy-MM-dd hh:mm'))
            }
          },
          {
            title: '修改时间',
            align: 'center',
            tooltip: true,
            render: (h, params) => {
              return h('div', this.$formatDate(new Date(params.row.createTime), 'yyyy-MM-dd hh:mm'))
            }
          },
          {
            title: '操作',
            key: 'action',
            width: 150,
            align: 'center',
            render: (h, params) => {
              return h('div', [
                h('Button', {
                  props: {
                    type: 'primary',
                    size: 'small'
                  },
                  style: {
                    marginRight: '5px'
                  },
                  on: {
                    click: () => {
                      this.editor()
                      let row = params.row
                      this.addForm.roleName = row.roleName
                      this.addForm.comments = row.comments
                      this.addForm.roleId = row.roleId
                    }
                  }
                }, '编辑'),
                h('Button', {
                  props: {
                    type: 'error',
                    size: 'small'
                  },
                  on: {
                    click: () => {
                      this.$Modal.confirm({
                        title: '你确定删除该标签吗？',
                        okText: '确定',
                        cancelText: '取消',
                        onOk: () => {
                          this.remove(params.row.roleId)
                        }
                      })
                    }
                  }
                }, '删除')
              ])
            }
          }
        ],
        roleList: []
      }
    },
    mounted() {
      this.rolePage()
    },
    methods: {
      rolePage() {
        this.$get(url, {
          keyword: this.keyword,
          pageSize: this.pageSize,
          pageNumber: this.pageNumber
        }).then((res) => {
          if (res.code === 0) {
            this.roleList = res.data
          }
        })
      },
      title(title) {
        this.modalTitle = title
        this.identity = true
      },
      add() {
        this.title('添加角色')
      },
      editor() {
        this.title('编辑角色')
      },
      submit() {
        if (this.modalTitle === '添加角色') {
          this.addOrg()
        } else {
          this.theEditor()
        }
      },
      addOrg() {
        this.$post(url, this.$qs.stringify(this.addForm)).then((res) => {
          if (res.code === 200) {
            this.$Message.success('添加成功')
            this.rolePage()
            this.cancel()
          }
        }).catch((rel) => {
          this.$Message.error('添加失败')
        })
      },
      theEditor() {
        this.$put(url, this.$qs.stringify(this.addForm)).then((res) => {
          if (res.code === 200) {
            this.$Message.success('修改成功')
            this.rolePage()
            this.cancel()
          }
        }).catch((rel) => {
          this.$Message.error('修改失败')
        })
      },
      remove(id) {
        this.$deletes(url + `/${id}`, {roleId: id}).then((res) => {
          if (res.code === 200) {
            this.$Message.success('删除成功')
            this.rolePage()
          }
        }).catch((rel) => {
          this.$Message.error('删除失败')
        })
      },
      searchOrg () {
        this.rolePage()
      },
      cancel() {
        this.addForm = {}
      },
      pageChange(data) {
        this.pageNumber = data
        this.rolePage()
      }
    },
    components: {
      PageCom
    }
  }
</script>

<style scoped>

</style>

